package cn.linjianhui.api.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class APIProxyFactory {
    private final static String PROJECTNAME = "cn.linjianhui.api.client";

    @Autowired
    private ProxyFactory ProxyFactory;

    private static String GetClientId(StackTraceElement[] stackTrace) {
        for (int i = stackTrace.length - 1; i >= 0; i--) {
            StackTraceElement element = stackTrace[i];
            int index = element.getClassName().indexOf(PROJECTNAME);
            if(index != -1) {
                return String.format("%s;Method:%s;Line:%s", element.getClassName().substring(index),
                        element.getMethodName(), element.getLineNumber());
            }
        }
        return null;
    }

    public <T> T CreateService(Class<T> tClass) {
        String clientId = GetClientId(Thread.currentThread().getStackTrace());
        return ProxyFactory.Create(tClass, clientId);
    }
}
